<template>
	<view>
		<!-- $cloudEnv_uid 当前用户的id -->
		<unicloud-db ref="db" :where="where" v-slot:default="{data, loading, error, options}"
			collection="guestbook,uni-id-users" field="_id,text,state,user_id.nickname,user_id.avatar_file,user_id._id">
			<view v-if="error">{{error.message}}</view>
			<view v-else>
				<view v-for="(item,index) in data" :title="item.user_id[0].nickname" note="">
					<img style="width: 50px;height: 50px" :src="item.user_id[0].avatar_file.url" alt="">
					{{item.user_id[0].nickname}}
					----{{item.state}}
					<button size="mini" @click="pass(item._id)">pass</button>
				</view>
			</view>
		</unicloud-db>
		<button @click="add">添加</button>
	</view>
</template>

<script>
	export default {
		data() {
			return {

			}
		},
		computed: {
			where() {
				if (this.uniIDHasRole('assessor')) {
					return ''
				} else {
					return "state==true || user_id._id ==$cloudEnv_uid"
				}
			}
		},
		methods: {
			add() {
				const db = uniCloud.database();
				db.collection('guestbook').add({
					'text': 'bbb',
					// 'state': true
					// 'user_id':'123'
				})

			},
			pass(id) {
				this.$refs.db.update(id, {
					state: true
				}, {
					complete: e => {
						this.$refs.db.refresh()
					}
				})
			}
		}
	}
</script>

<style>

</style>